#!/usr/bin/env python
# Copyright (c) tlpy.
# See LICENSE for details.

"""
Distutils installer for tlpy.
"""

import os
import sys

if sys.version_info < (2, 6):
    print("You must use at least Python 2.6 for tlpy", file=sys.stderr)
    sys.exit(3)

if os.path.exists('tlpy'):
    sys.path.insert(0, '.') # eek! need this to import tlpy. sorry.
from tlpy import copyright
from tlpy.python.dist import setup, ConditionalExtension as Extension
from tlpy.python.dist import getPackages, getDataFiles, getScripts
from tlpy.python.dist import tlpy_subprojects, _isCPython, _hasEpoll


extensions = [
    Extension("tlpy.test.raiser",
              ["tlpy/test/raiser.c"],
              condition=lambda _: _isCPython),

    Extension("tlpy.python._epoll",
              ["tlpy/python/_epoll.c"],
              condition=lambda builder: (_isCPython and _hasEpoll(builder) and
                                         sys.version_info[:2] < (2, 6))),

    Extension("tlpy.internet.iocpreactor.iocpsupport",
              ["tlpy/internet/iocpreactor/iocpsupport/iocpsupport.c",
               "tlpy/internet/iocpreactor/iocpsupport/winsock_pointers.c"],
              libraries=["ws2_32"],
              condition=lambda _: _isCPython and sys.platform == "win32"),

    Extension("tlpy.python.sendmsg",
              sources=["tlpy/python/sendmsg.c"],
              condition=lambda _: sys.platform != "win32"),
]

if sys.version_info[:2] <= (2, 6):
    extensions.append(
        Extension(
            "tlpy.python._initgroups",
            ["tlpy/python/_initgroups.c"]))


# Figure out which plugins to include: all plugins except subproject ones
subProjectsPlugins = ['tlpy_%s.py' % subProject
                      for subProject in tlpy_subprojects]
plugins = os.listdir(os.path.join(
    os.path.dirname(os.path.abspath(copyright.__file__)), 'plugins'))
plugins = [plugin[:-3] for plugin in plugins if plugin.endswith('.py') and
           plugin not in subProjectsPlugins]



setup_args = dict(
    # metadata
    name="tlpy Core",
    version=copyright.version,
    description="The core parts of the tlpy networking framework",
    author="tlpy",
    author_email="tlpy-python@tlpymatrix.com",
    maintainer="Glyph Lefkowitz",
    url="http://tlpymatrix.com/",
    license="MIT",
    long_description="""\
This is the core of tlpy, including:
 * Networking support (tlpy.internet)
 * Trial, the unit testing framework (tlpy.trial)
 * AMP, the Asynchronous Messaging Protocol (tlpy.protocols.amp)
 * tlpy Spread, a remote object system (tlpy.spread)
 * Utility code (tlpy.python)
 * Basic abstractions that multiple subprojects use
   (tlpy.cred, tlpy.application, tlpy.plugin)
 * Database connectivity support (tlpy.enterprise)
 * A few basic protocols and protocol abstractions (tlpy.protocols)
""",

    # build stuff
    packages=getPackages('tlpy',
                         ignore=tlpy_subprojects + ['plugins']),
    plugins=plugins,
    data_files=getDataFiles('tlpy', ignore=tlpy_subprojects),
    conditionalExtensions=extensions,
    scripts = getScripts(""),
)


if __name__ == '__main__':
    setup(**setup_args)
